package com.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.car.common.fastdfs.FastDFSClient;
import com.car.mapper.ComClassify2Mapper;
import com.car.model.common.dtos.ResponseResult;
import com.car.model.common.enums.AppHttpCodeEnum;
import com.car.model.component.pojos.ComClassify2;
import com.car.model.component.pojos.ComClassify2_img;
import com.car.service.ComClassify2Service;
import com.car.service.ComClassify2imgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class ComClassify2ServiceImpl extends ServiceImpl<ComClassify2Mapper, ComClassify2> implements ComClassify2Service {


    @Override
    public ResponseResult find2ClassifyById(Integer classifyId) {
        LambdaQueryWrapper<ComClassify2> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ComClassify2::getCl_id,classifyId);
        List<ComClassify2> list = list(wrapper);
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult delete(Integer id) {
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ComClassify2 classify = getById(id);
        if (classify == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        removeById(classify.getId());
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateClassify(ComClassify2 classify) {
        if (classify == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        updateById(classify);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    FastDFSClient fastDFSClient;

    @Value("${fdfs.url}")
    private String fileServerUrl;

    @Autowired
    ComClassify2imgService comClassify2imgService;

    @Override
    public ResponseResult saveClassify(ComClassify2 classify) {
        if (classify == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ComClassify2 one = getOne(Wrappers.<ComClassify2>lambdaQuery().eq(ComClassify2::getCode, classify.getCode()));
        if (one != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"二级分类编码重复,请重新输入");
        }
        //假数据,真实情况是从本地线程拿到当前登录用户id,然后根据id去存储图片的表里查询对应图片路径
        Integer userId = 1;
        ResponseResult<ComClassify2_img> result = comClassify2imgService.getImgByUserId(userId);
        classify.setImg(result.getData().getImg());
        save(classify);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        if (multipartFile == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        String fileId =null;
        try {
            fileId = fastDFSClient.uploadFile(multipartFile);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //假数据,真实情况从本地线程拿到当前登录用户id,根据id对图片进行存储,这里使用假数据
        ComClassify2_img classify2Img = new ComClassify2_img();
        classify2Img.setUser_id(1);
        classify2Img.setImg(fileServerUrl+fileId);
        comClassify2imgService.saveImg(classify2Img);
        return ResponseResult.okResult(fileId);
    }
}
